package com.youren.health.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.youren.health.constant.MessageConstant;
import com.youren.health.constant.RedisMessageConstant;
import com.youren.health.entity.Result;
import com.youren.health.pojo.Order;
import com.youren.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-30 11:03
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;


    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String, String> paraMap) {
        //获取redis连接
        Jedis jedis = jedisPool.getResource();
        //获取手机号
        String telephone = paraMap.get("telephone");
        //获取验证码的key
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //获取redis中的验证码
        String codeInRedis = jedis.get(key);//901800
        if (StringUtils.isEmpty(codeInRedis)) {
            return new Result(false, "验证码已过期，请重新获取验证码");
        }
        String validateCode = paraMap.get("validateCode");
        if (!validateCode.equals(codeInRedis)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //jedis.del(key);// 测试时可注释掉
        Integer id = orderService.submit(paraMap);
        return new Result(true, MessageConstant.ORDER_SUCCESS, id);
    }


    @GetMapping("/findById")
    public Result findById(Integer id) {
        Map<String, Object> orderInfo = orderService.findById(id);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderInfo);
    }
}
